<template>
  <div class="product-list">
    <h1>商品信息</h1>
    <div class="search-bar">
      <el-input
        v-model="searchParam.productName"
        placeholder="根据名称搜索"
        style="width: 200px; margin-right: 10px;"
        clearable
      />
      <el-select
        v-model="searchParam.foodTypeId"
        placeholder="根据食物类型搜索"
        style="width: 200px; margin-right: 10px;"
        clearable
      >
        <el-option
          v-for="type in foodTypes"
          :key="type.foodTypeId"
          :label="type.foodTypeName"
          :value="type.foodTypeId"
        />
      </el-select>
      <el-button type="primary" @click="getProducts">搜索</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </div>
    <el-table :data="products" style="width: 100%" stripe>
      <el-table-column prop="storeProductId" label="ID" width="100" />
      <el-table-column prop="productName" label="名称" width="180" />
      <el-table-column prop="price" label="价格" width="180" />
      <el-table-column prop="discount" label="价格折扣" width="180">
        <template #default="scope">
          {{ scope.row.discount==1?'没有折扣':scope.row.discount*10+"折"}}
        </template>
      </el-table-column>
      <el-table-column prop="foodTypeName" label="菜品类型" width="180" />
      <el-table-column prop="storeProductState" label="上架状态" width="180">
        <template #default="scope">
          {{ scope.row.storeProductState == 1 ? '上架' : '下架' }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="showDetail(scope.row)">详情</el-button>
          
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next, sizes, total"
        :total="total"
        :page-size="searchParam.pageSize"
        :current-page="searchParam.pageNum"
        :page-sizes="[2, 3, 5, 10]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 详情模态框 -->
    <el-dialog
      v-model="detailVisible"
      title="商品详情"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="detail-content">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="ID">
            <span class="detail-value">{{ currentProduct.storeProductId }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="名称">
            <span class="detail-value">{{ currentProduct.productName }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="价格">
            <span class="detail-value">{{ currentProduct.price }} 元</span>
          </el-descriptions-item>
          <el-descriptions-item label="价格折扣">
            <span class="detail-value">{{ currentProduct.discount }} 折</span>
          </el-descriptions-item>
          <el-descriptions-item label="菜品类型">
            <span class="detail-value">{{ currentProduct.foodTypeName }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="是否下架">
            <span class="detail-value">{{ currentProduct.storeProductState == 1 ? '是' : '否' }}</span>
          </el-descriptions-item>
        </el-descriptions>
        <div class="image-section">
          <h3>商品图片</h3>
          <el-image
            v-if="currentProduct.filePath"
            :src="currentProduct.filePath"
            :preview-src-list="[currentProduct.filePath]"
            fit="cover"
            style="width: 100%; max-height: 300px; border-radius: 8px;"
          />
          <el-empty v-else description="暂无图片" />
        </div>
      </div>
      <template #footer>
        <el-button type="primary" @click="detailVisible = false" style="position: relative;left: -800px;" >关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import { ElInput, ElButton, ElTable, ElTableColumn, ElPagination, ElMessage, ElSelect, ElOption, ElDialog, ElDescriptions, ElDescriptionsItem } from 'element-plus';

export default {
  components: {
    ElInput,
    ElButton,
    ElTable,
    ElTableColumn,
    ElPagination,
    ElSelect,
    ElOption,
    ElDialog,
    ElDescriptions,
    ElDescriptionsItem,
  },
  data() {
    return {
      products: [],
      searchParam: {
        storeId: localStorage.getItem("storeId"),
        pageNum: 1,
        pageSize: 5,
        productName: '',
        foodTypeId: '',
      },
      foodTypes: [], // 食物类型列表
      total: 0,
      detailVisible: false, // 控制模态框显示
      currentProduct: {}, // 当前选中的商品详情
    };
  },
  created() {
    this.getProducts();
    this.getFoodTypes(); // 获取食物类型
  },
  methods: {
    getProducts() {
      axios.post('/meals/storeProduct/searchStoreProductList', this.searchParam).then(res => {
        if (res.data.code === 200) {
          this.products = res.data.data.list;
          this.total = res.data.data.total;
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    },
    getFoodTypes() {
      axios.get('/meals/foodType/searchType', {
        params: { storeId: localStorage.getItem("storeId") }
      }).then(res => {
        if (res.data.code === 200) {
          this.foodTypes = res.data.data;
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    },
    showDetail(product) {
      this.currentProduct = product; // 设置当前商品详情
      this.detailVisible = true; // 显示模态框
    },
    deleteProduct(id) {
      console.log('删除商品ID:', id);
      this.products = this.products.filter(product => product.storeProductId !== id);
      axios.delete('/meals/storeProduct/deleteProduct', {
        params: { productId: id }
      }).then(res => {
        if (res.data.code === 200) {
          this.getProducts();
          ElMessage.success('删除成功');
        } else {
          ElMessage.error(res.data.msg);
        }
      });
      
    },
    resetSearch() {
      this.searchParam.productName = '';
      this.searchParam.foodTypeId = '';
      this.getProducts(); // 重置后重新加载数据
    },
    handleSizeChange(newSize) {
      this.searchParam.pageSize = newSize;
      this.searchParam.pageNum = 1; // 重置到第一页
      this.getProducts();
    },
    handleCurrentChange(newPage) {
      this.searchParam.pageNum = newPage;
      this.getProducts();
    },
  },
};
</script>

<style scoped>
.product-list {
  margin: 20px;
  font-family: Arial, sans-serif;
}

h1 {
  color: #333;
  text-align: center;
  margin-bottom: 20px;
}

.search-bar {
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

.el-input,
.el-select {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  margin-right: 10px;
  margin-bottom: 10px;
}

.el-button {
  border-radius: 4px;
}

.el-button--primary:hover {
  background-color: #409eff;
  border-color: #409eff;
}

.el-table {
  margin-top: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 模态框样式优化 */
.el-dialog {
  border-radius: 8px;
}

.detail-content {
  padding: 20px;
}

.el-descriptions {
  margin-top: 10px;
}

.el-descriptions-item__label {
  font-weight: bold;
  width: 100px;
  background-color: #f5f7fa;
  color: #606266;
}

.el-descriptions-item__content {
  padding-left: 20px;
  color: #303133;
}

.detail-value {
  font-size: 14px;
  color: #606266;
}

.el-dialog__footer {
  text-align: center;
}
</style>